package com.algorithm.cjm.july.chapter1.ch3;

/**
 * 字符串的全排练
 * 输入一个字符串，打印出该字符串的所有排列。例如，输入字符串“abc”, 则输出由字符 ‘a’ ‘b’ ‘c’ 所能排列出来的所有字符串
 * “abc”、“acb”、“bac”、“bca”、“cab” 和 “cba”
 *
 * Created by jieming.chen on 2020/1/12
 */
public class CalcAllPermutation {
    private static CalcAllPermutation obj = new CalcAllPermutation();

    public static void main(String[] args) {
        obj.CalcAllPermutation("abc".toCharArray(), 0, 2);
    }

    void CalcAllPermutation(char[] a, int from, int to) {
        if (to <= 1) {
            return;
        }
        if (from == to) {
            for (int i = 0; i <= to; i++) {
                System.out.print(a[i]);
            }
            System.out.println();
        } else {
            for (int i = from; i <= to; i++) {
                swap(a, i, from);
                CalcAllPermutation(a, from + 1, to);
                swap(a, i, from);
            }
        }
    }

    void swap(char[]a, int start, int end) {
        char c = a[start];
        a[start] = a[end];
        a[end] = c;
    }


}
